package com.adcc.ags.cgw.resources;

import com.adcc.ags.cgw.App;
import com.adcc.ags.cgw.configuration.BlackListFactory;
import com.adcc.ags.cgw.configuration.UserListFactory;
import com.adcc.ags.cgw.entity.user.GWMode;
import com.adcc.ags.cgw.managed.proxy.UserProxy;
import com.adcc.ags.cgw.managed.uplink.UplinkForward;
import com.adcc.ags.cgw.utils.Constant;
import com.adcc.ags.cgw.utils.FileUtil;
import com.adcc.ags.cgw.vo.ResultState;
import com.adcc.ags.cgw.vo.ResultVO;
import com.adcc.utility.log.Log;
import com.adcc.utility.xml.ADCCXML;
import com.adcc.utility.xml.ADCCXMLFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Optional;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.File;

/**
 * 网关接口
 */
@Path("/gw")
@Produces("application/json;charset=UTF-8")
public class GWResource {

    /**
     * 启动应急模式
     * @return
     * @throws JsonProcessingException
     */
    @GET
    @Path("/startEmergencyMode")
    public Optional<String> startEmergencyMode() throws JsonProcessingException {
        ResultVO resultVO = new ResultVO();
        try {

            // 合并黑名单
            String backupBlackListFile = App.confPath + Constant.STRING_SYNC_USERLIST_DIR + File.separator + Constant.STRING_BLACKLIST_FILE;
            if(FileUtil.isExist(backupBlackListFile)) {
                ADCCXML adccxml = ADCCXMLFactory.getInstance().createADCCXML();
                adccxml.parseXMLFile(backupBlackListFile);
                BlackListFactory.getInstance().updateBlackUserList(adccxml, true);
                Log.info(GWResource.class.getName(), "merge black user list success");
            }

            // 合并用户列表
            String backupUserListFile =  App.confPath + Constant.STRING_SYNC_USERLIST_DIR + File.separator + Constant.STRING_USERLIST_FILE;
            if(FileUtil.isExist(backupUserListFile)) {
                ADCCXML adccxml = ADCCXMLFactory.getInstance().createADCCXML();
                adccxml.parseXMLFile(backupUserListFile);
                UserListFactory.getInstance().updateUserList(adccxml, true);
                Log.info(GWResource.class.getName(), "merge user list success");
            }
            UplinkForward.getInstance().setMode(GWMode.EMERGENCY);
            UserProxy.getInstance().updateUserInfoMap(true);
            UserProxy.getInstance().setGWMode(GWMode.EMERGENCY);
            return Optional.of(resultVO.toJSON());
        } catch (Exception ex) {
            Log.error(GWResource.class.getName(), "openEmergencyMode() error", ex);
            resultVO.setResult(ResultState.FAILURE);
            return Optional.of(resultVO.toJSON());
        }
    }

    /**
     * 关闭应急模式
     * @return
     * @throws JsonProcessingException
     */
    @GET
    @Path("/closeEmergencyMode")
    public Optional<String> closeEmergencyMode() throws JsonProcessingException {
        ResultVO resultVO = new ResultVO();
        try{
            UserListFactory.getInstance().deleteAllUser(true);
            BlackListFactory.getInstance().deleteAllBlackListUser(true);

            UplinkForward.getInstance().setMode(GWMode.NORMAL);
            UserProxy.getInstance().updateUserInfoMap(true);
            UserProxy.getInstance().setGWMode(GWMode.NORMAL);
            return Optional.of(resultVO.toJSON());
        }catch (Exception ex){
            Log.error(GWResource.class.getName(), "closeEmergencyMode() error", ex);
            resultVO.setResult(ResultState.FAILURE);
            return Optional.of(resultVO.toJSON());
        }
    }
}
